package com.kk.score.service.impl;


import com.kk.score.entity.vo.UserVo;
import com.kk.score.utils.CrawlerUtils;
import com.kk.score.utils.HttpClientConfig;
import org.apache.commons.httpclient.NameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * @author KK
 * @CreateTime 2022-08-29
 */
@Service
public class JWCJLoginServiceImpl {

    public String isT(String userAccount, String userPassword, String encoded) {
        // 1
        String loginUrl = "http://jwci.pzhu.cn/jsxsd/xk/LoginToXk";
        // 设置登陆时要求的信息，用户名和密码
        NameValuePair[] loginInfo = {new NameValuePair("userAccount", userAccount),
                new NameValuePair("userPassword", userPassword),
                new NameValuePair("loginMethod", "LoginToXk"),
                new NameValuePair("encoded", encoded)};
        try {
            String cookie = CrawlerUtils.post(loginUrl, loginInfo);
            Map<String, String> map = new HashMap<>();
            map.put("Cookie", cookie);
            Document user = Jsoup.parse(HttpClientConfig.doGetInfo(cookie));
            Element is = user.selectFirst("#Form1 > table:nth-child(4) > tbody > tr:nth-child(1) > td:nth-child(5)");
            return Objects.isNull(is) ? "false" : cookie;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "false";
    }

    public UserVo getInfo(String cookie) {
        UserVo userVo = new UserVo();
        //表单网页
        Document user = Jsoup.parse(HttpClientConfig.doGetInfo(cookie));
        Element is = user.selectFirst("#Form1 > table:nth-child(4) > tbody > tr:nth-child(15) > td:nth-child(3)");
        if (Objects.isNull(is)) {
            return null;
        }
        //部门
        String department = user.selectFirst("#Form1 > table:nth-child(4) > tbody > tr:nth-child(15) > td:nth-child(3)").text().replaceAll("\\*", "").replaceAll(" ", "");
        //性别
        String sex = user.selectFirst("#Form1 > table:nth-child(4) > tbody > tr:nth-child(3) > td:nth-child(2)").text().replaceAll("\\*", "").replaceAll(" ", "");
        //姓名
        String name = user.selectFirst("#Form1 > table:nth-child(4) > tbody > tr:nth-child(1) > td:nth-child(5)").text().replaceAll("\\*", "").replaceAll(" ", "");
        //职位
        String position = user.selectFirst("#Form1 > table:nth-child(4) > tbody > tr:nth-child(7) > td:nth-child(5)").text().replaceAll("\\*", "").replaceAll(" ", "");
        userVo.setDepartment(department);
        userVo.setPosition(position);
        userVo.setName(name);
        userVo.setSex(sex);
        return userVo;
    }


}
